<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <div :style="{ display: 'flex', alignItems: 'center', borderBottom: '1px solid #ccc' }">
          <div class="tou_zuo">
            <img src="/src/assets/images/logo.png" alt="" />
            <span>BIG CUTE</span>
          </div>
          <div class="tou_you">
            <div class="mian" :style="{ display: 'flex' }">
              <div :style="{ marginRight: '20px', marginLeft: '20px' }">
                <el-icon><Fold /></el-icon>
              </div>
              <!-- 面包屑 -->
              <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/' }">home</el-breadcrumb-item>
                <!-- 循环遍历 -->
                <el-breadcrumb-item v-for="(item, index) in maindata" :key="index">
                  <a :href="item.href">{{ item.title }}</a>
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </div>
          <div class="icon">
            <el-icon><Search /></el-icon>
            <el-icon @click="onload"><Refresh /></el-icon>
            <el-icon><Bell /></el-icon>
            <el-icon><FullScreen /></el-icon>
          </div>
          <div class="tx">
            <div>
              <el-popover :visible="visible" placement="top" :width="160">
                <p>账号：{{ user.user.username }}</p>
                <p>权限：{{ user.rolename }}</p>
                <div style="text-align: right; margin: 0; display: 'flex'">
                  <el-button size="small" text @click="visible = false">取消</el-button>
                  <el-button size="small" type="primary" @click="tc"> 退出登录 </el-button>
                </div>
                <template #reference>
                  <el-avatar :size="70" :src="user.user.img" @click="visible = true" />
                </template>
              </el-popover>
            </div>
            <div class="wz">
              {{ user.user.username }}
            </div>
            <div>
              <el-icon><Setting /></el-icon>
            </div>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="270px">
          <el-row class="tac">
            <el-col :span="24">
              <el-menu
                :default-active="activeIndex"
                class="el-menu-vertical-demo"
                @select="selectedandle"
              >
                <div v-for="(item, index) in homeRouter" :key="index">
                  <el-sub-menu :index="index + ''">
                    <template #title>
                      <router-link :to="item.path">
                        <el-icon v-if="index == 0"><HomeFilled /></el-icon>
                        <el-icon v-if="index == 1"><UserFilled /></el-icon>
                        <el-icon v-if="index == 2"><Checked /></el-icon>
                        <el-icon v-if="index == 3"><Histogram /></el-icon>
                        <el-icon v-if="index == 4"><UserFilled /></el-icon>
                        <!-- 标签 -->
                        <span @click="mian(item, index)">{{ item.title }}</span>
                      </router-link>
                    </template>
                    <el-menu-item-group title="" v-if="item.children">
                      <!-- 子元素 -->
                      <div v-for="(v, i) in item.children" :key="i">
                        <router-link :to="v.path">
                          <el-menu-item :index="'child-' + index + '-' + i"
                            ><el-icon><Menu /></el-icon
                            ><span @click="mian(v, i)">{{ v.title }}</span></el-menu-item
                          >
                        </router-link>
                      </div>
                    </el-menu-item-group>
                  </el-sub-menu>
                </div>
              </el-menu>
            </el-col>
          </el-row>
        </el-aside>
        <el-main>
          <!-- 右边  -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { homeRouter } from '../router/index'
import {
  HomeFilled,
  UserFilled,
  Menu,
  Checked,
  Histogram,
  Fold,
  Search,
  Refresh,
  Bell,
  FullScreen,
  Setting
} from '@element-plus/icons-vue'

// console.log(homeRouter)

import { Document, Menu as IconMenu, Location } from '@element-plus/icons-vue'
const user = localStorage.getItem('counter')
  ? JSON.parse(localStorage.getItem('counter') as string)
  : ''
const visible = ref(false)
const router = useRouter()
const route = useRoute()
//高亮
// const storedIndex = localStorage.getItem('activeMenuIndex')
const initIndex = localStorage.getItem('initIndex')
  ? localStorage.getItem('initIndex')
  : 'child-1-0'
const activeIndex = ref(initIndex)
//
const selectedandle = (index: any) => {
  console.log('selected', index)
  localStorage.setItem('initIndex', index)
}
//高亮
// const activeIndex = computed(() => {
//   const matched = homeRouter.find((item: any) => item.path === route.path)
//   if (matched) {
//     return homeRouter.indexOf(matched).toString()
//   }
//   return '0'
// })

//面包屑
const maindata = ref([
  {
    title: '首页',
    href: '/home'
  }
])
//退出登录
const tc = () => {
  visible.value = false
  localStorage.removeItem('token')
  router.push('/login')
}
//面包屑
const mian = (val: any, index: number) => {
  // console.log(val, index)
  let flag = maindata.value.find((item) => item.title == val.title)
  if (!flag) {
    maindata.value.push({
      title: val.title,
      href: val.path
    })
  }
}
//icon刷新
const onload = () => {
  window.location.reload()
}
//放大
</script>

<style scoped lang="scss">
.common-layout {
  width: 100%;
  height: 100%;
}
.el-header {
  width: 100%;
  height: 80px;
}
.el-aside {
  height: 1000px;
}
.tou_zuo {
  width: 250px;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  img {
    width: 80px;
    height: 60px;
  }
  span {
    font-size: 30px;
    font-weight: bolder;
  }
}
.tou_you {
  clear: both;
}
.tx {
  position: absolute;
  right: 30px;
  display: flex;
  align-items: center;
  top: 0;
  .el-icon {
    margin-left: 10px;
    font-size: 20px;
  }
  .wz {
    font-size: 26px;
    font-weight: bolder;
  }
}
.icon {
  position: absolute;
  right: 230px;
  .el-icon {
    margin: 0px 10px;
    font-size: 20px;
    cursor: pointer;
  }
}
</style> 